package com.lishu.carsafe.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.lishu.carsafe.R;
import com.lishu.carsafe.adapter.TaskListAdapter;
import com.lishu.carsafe.http.HttpBase;
import com.lishu.carsafe.http.HttpLoader;
import com.lishu.carsafe.listener.DateSearchListener;
import com.lishu.carsafe.model.BaseModel;
import com.lishu.carsafe.model.TaskModel;
import com.lishu.carsafe.utils.ClickUtil;
import com.lishu.carsafe.utils.DateSearchUtil;
import com.lishu.carsafe.utils.ListUtil;
import com.lishu.carsafe.utils.StringUtil;
import com.lishu.carsafe.utils.ToastUtil;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;

import java.util.ArrayList;
import java.util.List;

public class TaskActivity extends BaseSearchActivity implements View.OnClickListener {
    private EditText begin_time_ev, end_time_ev;
    private String chooseBeginTime, chooseEndTime;
    private ListView task_list;
    private TaskListAdapter mTaskListAdapter;
    private int curPage;
    private SmartRefreshLayout refreshLayout;
    private LinearLayout no_unprocessed_task_layout, no_processed_task_layout;
    private List<TaskModel.TaskBean> mProcessedTaskList = new ArrayList<>();
    private List<TaskModel.TaskBean> mUnprocessedTaskList = new ArrayList<>();
    //任务tab栏
    private final int PRESSED_TEXT_COLOR = 0xff000000;
    private final int NORMAL_TEXT_COLOR = 0xff666666;
    private int curTab;
    private LinearLayout processedLayout;
    private LinearLayout unprocessedLayout;
    private TextView processedTaskTV;
    private TextView unprocessedTaskTV;
    private View processedBar;
    private View unprocessedBar;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_task);

        curTab = 0;
        chooseBeginTime = "";//TimeUtil.getCurDatetime();
        chooseEndTime = "";//TimeUtil.getCurDatetime();

        initView();
        initEvent();
        showTaskList();
    }

    @Override
    protected void onResume() {
        super.onResume();
        getTaskListByTime(1, chooseBeginTime, chooseEndTime);
        //testData();
    }

    private void initView() {
        setTopTitle(R.string.module_task);
        begin_time_ev = findViewById(R.id.begin_time_ev);
        end_time_ev = findViewById(R.id.end_time_ev);
        task_list = findViewById(R.id.task_list);
        processedLayout = findViewById(R.id.processed_layout);
        unprocessedLayout = findViewById(R.id.unprocessed_layout);
        processedTaskTV = findViewById(R.id.processed_task);
        unprocessedTaskTV = findViewById(R.id.unprocessed_task);
        processedBar = findViewById(R.id.processed_bar);
        unprocessedBar = findViewById(R.id.unprocessed_bar);

        mTaskListAdapter = new TaskListAdapter(this);
        task_list.setAdapter(mTaskListAdapter);

        refreshLayout = findViewById(R.id.refreshLayout);
        refreshLayout.setEnableRefresh(false);
        no_unprocessed_task_layout = findViewById(R.id.no_unprocessed_task_layout);
        no_processed_task_layout = findViewById(R.id.no_processed_task_layout);
    }

    private void initEvent() {
        begin_time_ev.setOnClickListener(this);
        end_time_ev.setOnClickListener(this);
        processedLayout.setOnClickListener(this);
        unprocessedLayout.setOnClickListener(this);
        findViewById(R.id.search_button).setOnClickListener(this);
        task_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long l) {
                String taskId = ((TaskModel.TaskBean)parent.getItemAtPosition(position)).getId();
                Intent intent = new Intent();
                intent.putExtra("taskId", taskId);
                if(curTab == 0) {
                    intent.setClass(TaskActivity.this, TaskUnprocessedDetailActivity.class);
                }else{
                    intent.setClass(TaskActivity.this, TaskProcessedDetailActivity.class);
                }
                startActivity(intent);
            }
        });
        refreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                curPage++;
                if (StringUtil.isEmpty(chooseBeginTime) && StringUtil.isEmpty(chooseEndTime)) {
                    getTaskList("", "", curPage, COUNT_PER_PAGE);
                }else {
                    /*getTaskList(chooseBeginTime + "000000",
                            chooseEndTime + "235959",
                            curPage, COUNT_PER_PAGE);*/
                    getTaskList(chooseBeginTime, chooseEndTime,
                            curPage, COUNT_PER_PAGE);
                }
                refreshLayout.finishLoadMore();
            }
        });
    }

    private void testData() {
        //@@@@@@@@@@@@@@@@@@ just for testing, begin @@@@@@@@@@@@@@@@@
        List<TaskModel.TaskBean> testList = new ArrayList<>();
        testList.add(new TaskModel().new TaskBean("1","任务名称1", "我公司在今天下午进行员工总结大会", "20181209121514", "0"));
        testList.add(new TaskModel().new TaskBean("2","任务名称2", "城管局如何做好巡检工作", "20181209101510", "0"));
        testList.add(new TaskModel().new TaskBean("3","任务名称3", "冬季如何做好防护工作", "20181209111836",  "1"));
        testList.add(new TaskModel().new TaskBean("4","任务名称4", "我公司在今天下午进行员工总结大会，请全体人员都准时参加活动", "20181216091842", "1"));
        //处理状态(0:未处理，1;已处理)
        for(int i = 0; i < testList.size(); i++){
            if("1".equals(testList.get(i).getState())){
                mProcessedTaskList.add(testList.get(i));
            }else{
                mUnprocessedTaskList.add(testList.get(i));
            }
        }
        mTaskListAdapter.setData(mUnprocessedTaskList);
        //@@@@@@@@@@@@@@@@@@ just for testing, end @@@@@@@@@@@@@@@@@@
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.begin_time_ev:
                showDatePickerDialog(0);
                break;
            case R.id.end_time_ev:
                showDatePickerDialog(1);
                break;
            case R.id.unprocessed_layout:
                curTab = 0;
                setTab(0);
                showTaskList();
                break;
            case R.id.processed_layout:
                curTab = 1;
                setTab(1);
                showTaskList();
                break;
            case R.id.search_button:
                if (ClickUtil.isFastClick()) {
                    return ;
                }
                search();
                break;
        }
    }

    private void setTab(int i) {
        switch (i) {
            case 0:
                processedBar.setVisibility(View.INVISIBLE);
                unprocessedBar.setVisibility(View.VISIBLE);
                processedTaskTV.setTextColor(NORMAL_TEXT_COLOR);
                unprocessedTaskTV.setTextColor(PRESSED_TEXT_COLOR);
                break;
            case 1:
                processedBar.setVisibility(View.VISIBLE);
                unprocessedBar.setVisibility(View.INVISIBLE);
                processedTaskTV.setTextColor(PRESSED_TEXT_COLOR);
                unprocessedTaskTV.setTextColor(NORMAL_TEXT_COLOR);
                break;
            default:
                break;
        }
    }

    private void search() {
        final String beginDate = begin_time_ev.getText().toString();
        final String endDate = end_time_ev.getText().toString();
        DateSearchUtil.searchByDate(beginDate, endDate, new DateSearchListener() {
            @Override
            public void searchByDefaultDate() {
                getTaskListByTime(1, "", "");
            }

            @Override
            public void searchByChooseDate(String beginDate, String endDate) {
                getTaskListByTime(1, beginDate, endDate);
            }
        });
    }

    private void getTaskListByTime(int page, String beginDate, String endDate){
        if(page == 1){
            curPage = 1;
            mProcessedTaskList.clear();
            mUnprocessedTaskList.clear();
        }
        chooseBeginTime = beginDate;
        chooseEndTime = endDate;

        if (StringUtil.isEmpty(beginDate) && StringUtil.isEmpty(endDate)){
            getTaskList("", "", curPage, COUNT_PER_PAGE);
        }else {
            /*getTaskList(beginDate + "000000",
                    endDate + "235959",
                    curPage, COUNT_PER_PAGE);*/
            getTaskList(beginDate, endDate,
                    curPage, COUNT_PER_PAGE);
        }
    }

    private void getTaskList(String beginTime, String endTime, int curPage, final int count){
        String bTime = beginTime.replace("/", "-");
        String eTime = endTime.replace("/", "-");
        HttpLoader.getTaskList(bTime, eTime, curPage, count, new HttpBase.IResponseListener() {
            @Override
            public void onResponse(BaseModel model) {
                if (model == null) {
                    ToastUtil.showShort(R.string.please_check_network);
                    return;
                }
                if (!model.success()) {
                    mProcessedTaskList.clear();
                    mUnprocessedTaskList.clear();
                    mTaskListAdapter.setData(mUnprocessedTaskList);
                    mTaskListAdapter.setData(mProcessedTaskList);
                    ToastUtil.showShort(getString(R.string.get_data_fail) + model.getMsg());
                    return;
                }

                List<TaskModel.TaskBean> taskList = ((TaskModel) model).getData();
                if (taskList != null) {
                    //处理状态(0:未处理，1;已处理)
                    for(int i = 0; i < taskList.size(); i++){
                        if("0".equals(taskList.get(i).getState()) || "2".equals(taskList.get(i).getState())){
                            mUnprocessedTaskList.add(taskList.get(i));
                        }else{
                            mProcessedTaskList.add(taskList.get(i));
                        }
                    }
                }else{
                    ToastUtil.showShort(R.string.no_more_data);
                }
                showTaskList();
            }
        });
    }

    private void showTaskList(){
        if(curTab == 0){
            if(ListUtil.isEmpty(mUnprocessedTaskList)){
                no_unprocessed_task_layout.setVisibility(View.VISIBLE);
                no_processed_task_layout.setVisibility(View.GONE);
                refreshLayout.setVisibility(View.GONE);
            }else{
                no_unprocessed_task_layout.setVisibility(View.GONE);
                no_processed_task_layout.setVisibility(View.GONE);
                refreshLayout.setVisibility(View.VISIBLE);
                mTaskListAdapter.setData(mUnprocessedTaskList);
            }
        }else{
            if(ListUtil.isEmpty(mProcessedTaskList)){
                no_unprocessed_task_layout.setVisibility(View.GONE);
                no_processed_task_layout.setVisibility(View.VISIBLE);
                refreshLayout.setVisibility(View.GONE);
            }else{
                no_unprocessed_task_layout.setVisibility(View.GONE);
                no_processed_task_layout.setVisibility(View.GONE);
                refreshLayout.setVisibility(View.VISIBLE);
                mTaskListAdapter.setData(mProcessedTaskList);
            }
        }
    }

}
